#!/bin/bash

# This is just a proof of concept on extract_str.c, which was extracted from str.c
# substitute "extract_str" with any source file in general

# Setup Requirements:
# 1. Klee, llvm and llvm-gcc installed according to README
# 2. Replace main.cpp and executor.cpp in klee
# 2. mysql installed
# 3. ILAA table need to be created before using this script.
# If not, use the following 2 lines:
# mysqladmin -u<root> -p<password> create ILAA  #substitute with your username and password
# mysql -u<root> -p<password> < create_ilaa2.sql

# using llvm to compile into bytecode extract_str.o
llvm-gcc --emit-llvm -c -g extract_str.c

# using llvm-gcc to generate assembly file in text form from bytecode
rm -f extract_str.o.ll; llvm-dis extract_str.o

# static analysis. detect loops in program and generate all basic block cycles on each loop found.
./detectloop_cycles_nogcov.sh extract_str.c extract_str.o.ll cycle 2>/dev/null > extract_str.cycles

# generate loopmap file to feed into klee, so that it knows where are the loops
./formatLoopMap3 extract_str.cycles > extract_str.loopmap
#./formatLoopMap4 extract_str.cycles > extract_str.loopmap

# auto exploration, output trace using stderr
klee  --libc=uclibc --posix-runtime --loopmap-path=./extract_str.loopmap ./extract_str.o 2>extract_str.trace

# format the trace output from stderr into a nice form to import into mysql
echo extract_str.trace > tracelist; ./formatall tracelist | grep -n "." > extract_str.analysis.in

# prepare mysql import file from template file
cat import_template.sql | sed -e "s/ANALYSIS.IN/extract_str.analysis.in/" > import.sql

# import data into mysql. This is a sample password and username. Pls substitute with your own
mysql -uroot -ppassword -D ILAA -B -e "delete from ILAA"; mysql -uroot -ppassword ILAA < import.sql

# Use cycles file to generate .sqlin file, used to analyze each loop that we found from static anlaysis
./formatLoopMap_sqlin extract_str.cycles

# analyze each loop and output dependency matrix
./ilaa_db8_main . extract_str.cycles.sqlin  extract_str
